package com.filldream.fastboot.core.manager;

import com.filldream.fastboot.base.entity.Menu;
import com.filldream.fastboot.base.entity.Role;
import com.filldream.fastboot.base.mapper.MenuMapper;
import com.filldream.fastboot.base.mapper.RoleMenuMapper;
import com.filldream.fastboot.base.response.pc.MenuDTO;
import com.filldream.fastboot.base.response.pc.MenusDTO;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.common.util.TransferUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class RoleMenuManager {

    /**
     * 菜单与角色规约
     * Role的PID为父级角色ID，若为0表示超级管理员角色，有且只有一个；
     */

    @Resource
    private MenuManager menuManager;

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private RoleManager roleManager;

    //---------FastBoot Menu 3.0 ---------



    //---------End FastBoot Menu 3.0 ---------



    //---------FastBoot Menu Update---------

    //获取角色已选菜单
    public MenusDTO getRoleSelectedMenu(Long roleId){

        MenusDTO result = new MenusDTO();
        List<Menu> menus ;
        if( roleId.equals(1L) ){
            menus = menuManager.getAllMenuList();
        }else{
            menus = roleMenuMapper.getMenuListByRoleId(roleId);
        }

        List<Long> menuIds = menus.stream().map(Menu::getId).collect(Collectors.toList());
        result.setMenus(menuIds);
        List<MenuDTO> menuList = menuManager.toTreeList(TransferUtil.changeToList(menus, MenuDTO.class),null);
        result.setMenuList(menuList);

        Map<Long, Menu> map = menus.stream().collect(Collectors.toMap(Menu::getId, Function.identity()));

        return screenMenusDTO(result, map);
    }


    //获取角色的可选菜单(即父级的菜单中选择)
    public MenusDTO getRoleOptionalMenu(boolean isAdd , Long roleId){

        //查询父级的权限
        Role nowRole = roleManager.getById(roleId);
        Assert.isNull(nowRole,ErrorCode.AUTH_ERR.reMsg(":当前角色不存在"));

        //查询父级角色
        Long parentId = isAdd ? nowRole.getId() : nowRole.getPId();

        MenusDTO parentMenus = getRoleSelectedMenu(parentId.equals(0L) ? roleId : parentId);


        //父级树状图
        List<MenuDTO> parentTree = parentMenus.getMenuList();

        //查询自己的权限
        MenusDTO myMenus  = parentId.equals(0L) ? parentMenus : getRoleSelectedMenu(roleId);

        List<Long> noMenuIds = new ArrayList<>();
        List<Long> selectIds = myMenus.getMenus();
        for(MenuDTO menuDTO : parentTree){
            menuDTO.setIsCheck(null);
            List<MenuDTO> childList = menuDTO.getChildren();
            if(childList != null && !childList.isEmpty()){
                for(MenuDTO child : childList){
                    child.setIsCheck(null);
                    List<MenuDTO> childList2 = child.getChildren();
                    if(childList2 != null && childList2.isEmpty() == false){
                        for(MenuDTO child2 : childList2){
                            child2.setIsCheck( selectIds.contains(child2.getId()) );
                            if(child2.getIsCheck() && child2.getIsMenu() == 0){
                                noMenuIds.add(child2.getId());
                            }
                        }
                    }else{
                        child.setIsCheck( selectIds.contains(child.getId())  );
                        if(child.getIsCheck() && child.getIsMenu() == 0){
                            noMenuIds.add(child.getId());
                        }
                    }
                }
            }
        }

        MenusDTO result = new MenusDTO();
        result.setMenuList(parentTree);
        result.setMenus(myMenus.getMenus());
        result.setNoMenuIds(noMenuIds);
        return result;

    }



    public MenusDTO screenMenusDTO( MenusDTO menusDTO, Map<Long, Menu> map ){

        List<Long> menus = menusDTO.getMenus();

        //菜单
        List<Menu> isMenuList = new ArrayList<>();
        List<Long> isMenuIdList = new ArrayList<>();

        //非菜单
        List<Menu> buttonList = new ArrayList<>();
        List<Long> buttonIdList = new ArrayList<>();

        //code集
        List<String> codeList = new ArrayList<>();
        //url集合
        List<String> urlList = new ArrayList<>();


        //递归查找菜单
        for (Long id : menus) {
            Menu menu =map.get(id);
            if(StringUtils.isNotBlank(menu.getCode())){
                codeList.add(menu.getCode());
            }
            if(StringUtils.isNotBlank(menu.getMenuUrl())){
                urlList.add(menu.getMenuUrl());
            }
            if( menu.getIsMenu().equals(1) ){
                isMenuList.add(menu);
                isMenuIdList.add( menu.getId() );
            }else{
                buttonList.add(menu);
                buttonIdList.add( menu.getId() );
            }
        }
        //组合菜单
        List<MenuDTO> list = menuManager.toTreeList(isMenuList, isMenuIdList);

        //返回
        menusDTO.setYesMenu(  list );
        menusDTO.setYesMenuIds( isMenuIdList );
        menusDTO.setNoMenu( buttonList );
        menusDTO.setNoMenuIds( buttonIdList );
        menusDTO.setCodes(codeList);
        menusDTO.setUrls(urlList);
        return menusDTO;
    }



}
